package com.atguigu.my.chapter02

import scala.util.control.BreakControl

/**
 *
 * @description: xxx
 * @time: 2020-05-22 11:26
 * @author: baojinlong 
 **/
object FunctionDemo02 {
  def main(args: Array[String]): Unit = {
    val n1 = 10
    val n2 = 20
    println("结果是=" + getRes2(n1, n2, '+'))
    test(4)
    test2(4)
  }

  /**
   * 这种写法会出现类型不兼容的问题
   *
   * @param n1
   * @param n2
   * @param oper
   * @return
   */
  //  def getRes(n1: Int, n2: Int, oper: Char): Int = {
  //    if ('+'.equals(oper)) {
  //      n1 + n2
  //    } else if ('-'.equals(oper)) {
  //      n1 - n2
  //    } else {
  //      null
  //    }
  //  }

  def getRes2(n1: Int, n2: Int, oper: Char): Any = {
    if ('+'.equals(oper)) {
      n1 + n2
    } else if ('-'.equals(oper)) {
      n1 - n2
    } else {
      null
    }
  }

  /**
   * 递归
   */
  def test(n: Int): Unit = {
    if (n > 2) {
      test(n - 1)
    }
    println("n=" + n)
  }

  @scala.annotation.tailrec
  def test2(n: Int): Unit = {
    if (n > 2) {
      test2(n - 1)
    } else {
      println("n=" + n)
    }
  }
}
